<?php

namespace App\Http\Controllers\Administrator\Game;

use App\Models\Def\MainGamePlat;
use Carbon\Carbon;
use App\Models\Conf\Site;
use Illuminate\Http\Request;
use App\Models\Log\PlayerBetFlow;
use App\Http\Controllers\Controller;
use App\Http\Requests\Administrator\PlayerBetFlowRequest;

class PlayerBetFlowController extends Controller
{
    /**
     * gameapi
     * 2018/10/8 19:43
     * Administrator
     * index
     * Display a listing of the resource.
     * @param PlayerBetFlowRequest $request
     * @param PlayerBetFlow $playerBetFlow
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index(PlayerBetFlowRequest $request, PlayerBetFlow $playerBetFlow)
    {
        $this->authorize('index', $playerBetFlow);

        $startTime = empty($request->only('bet_start_date')) ? Carbon::now()->startOfWeek()->toDateTimeString() : $request->bet_start_date;
        $endTime = empty($request->only('bet_end_date')) ? Carbon::now()->toDateTimeString() : $request->bet_end_date;
        $where = [
            ['bet_time', '>=', $startTime],
            ['bet_time', '<=', $endTime],
        ];
        $params = [
            'bet_start_date' => $startTime,
            'bet_end_date' => $endTime,
            'username' => '',
            'site_username' => '',
            'main_game_plat_code' => '',
            'game_flow_code' => '',
        ];

        if (!empty($request->username)) {
            $where[] = ['username', '=', $request->username];
            $params['username'] = $request->username;
        }
        if (!empty($request->site_username)) {
            $where[] = ['site_username', '=', $request->site_username];
            $params['site_username'] = $request->site_username;
        }
        if (!empty($request->main_game_plat_code)) {
            $where[] = ['main_game_plat_code', '=', $request->main_game_plat_code];
            $params['main_game_plat_code'] = $request->main_game_plat_code;
        }
        if (!empty($request->game_flow_code)) {
            $where[] = ['game_flow_code', '=', $request->game_flow_code];
            $params['game_flow_code'] = $request->game_flow_code;
        }
        $siteNames = $this->getCurrentUserSite();

        $playerBetFlow = PlayerBetFlow::where($where)->whereIn('site_username', $siteNames)->orderBy('bet_time', 'desc');

        $sumParams['bet_amount'] = $playerBetFlow->sum('bet_amount');
        $sumParams['available_bet_amount'] = $playerBetFlow->sum('available_bet_amount');
        $sumParams['company_payout_amount'] = $playerBetFlow->sum('company_payout_amount');
        $sumParams['company_win_amount'] = $playerBetFlow->sum('company_win_amount');

        $limit = $request->limit ?? config('administrator.paginate.limit');
        $playerBetFlows = $playerBetFlow->paginate((integer)$limit);

        $plats = MainGamePlat::whereHas('site', function ($query) use ($siteNames) {
            $query->whereIn('conf_sites.username', $siteNames)->where('conf_site_main_game_plats.status', 1);
        }
        )->status()->get();


        return view('backend.game.player_bet_flow.index', compact('playerBetFlows', 'params', 'sumParams', 'limit', 'plats'));
    }

    /**
     * gameapi
     * 2018/12/20 22:34
     * Administrator
     * create
     *
     * @param PlayerBetFlow $playerBetFlow
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create(PlayerBetFlow $playerBetFlow)
    {
        $this->authorize('manage_player_bet_flow_manager', $playerBetFlow);
        $sites=Site::all();
        return view('backend.game.player_bet_flow.create_and_edit',compact('playerBetFlow','plats','sites'));
    }

    /**
     * gameapi
     * 2018/12/20 22:34
     * Administrator
     * store
     * Store a newly created resource in storage.
     * @param PlayerBetFlowRequest $request
     * @param PlayerBetFlow $playerBetFlow
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(PlayerBetFlowRequest $request,PlayerBetFlow $playerBetFlow)
    {
        $this->authorize('manage_player_bet_flow_manager', $playerBetFlow);
        $data=$request->validated();
        PlayerBetFlow::create([$data]);

        return redirect()->route('player_bet_flow.index')->with('success', '创建成功.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {

    }

    /**
     * gameapi
     * 2018/12/20 22:34
     * Administrator
     * edit
     * Show the form for editing the specified resource.
     * @param PlayerBetFlow $playerBetFlow
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(PlayerBetFlow $playerBetFlow)
    {
        $this->authorize('manage_player_bet_flow_manager', $playerBetFlow);
        $sites=Site::all();
        return view('backend.game.player_bet_flow.create_and_edit',compact('playerBetFlow','plats','sites'));
    }

    /**
     * gameapi
     * 2018/12/20 22:34
     * Administrator
     * update
     * Update the specified resource in storage.
     * @param PlayerBetFlowRequest $request
     * @param PlayerBetFlow $playerBetFlow
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(PlayerBetFlowRequest $request, PlayerBetFlow $playerBetFlow)
    {
        $this->authorize('manage_player_bet_flow_manager', $playerBetFlow);
        $data=$request->validated();
        $playerBetFlow->timestamps=false;
        $playerBetFlow->update($data);
        return redirect()->route('player_bet_flow.index')->with('success', '修改成功.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * gameapi
     * 2018/10/8 12:27
     * Administrator
     * getCurrentUserSite
     * 获取当前用户网站id
     * @return array
     */
    public function getCurrentUserSite()
    {
        $user = \Auth::user();
        $type = $user->type;
        if ($type === 0) {
            $siteNames = Site::all()->pluck('username')->toArray();
            $siteNames[] = 'admin';
        } elseif ($type == 1) {
            $siteNames = $user->agent->sites()->get()->pluck('username')->toArray();
        } elseif ($type = 2) {
            $siteNames = $user->site()->get()->pluck('username')->toArray();
        }
        //添加记录默认网站名

        return $siteNames;
    }
}
